package com.farm.learn.controller;

import com.farm.core.page.OperateType;
import com.farm.core.page.RequestMode;
import com.farm.core.page.ViewMode;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.learn.domain.ClassComment;
import com.farm.learn.domain.Classt;
import com.farm.learn.service.ClassCommentServiceInter;
import com.farm.learn.service.ClasstServiceInter;
import com.farm.web.WebUtils;
import com.farm.web.easyui.EasyUiUtils;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@RequestMapping("/classcomment")
@Controller
public class ClassCommentController extends WebUtils {
    private final static Logger log = Logger.getLogger(ClassCommentController.class);
    @Resource
    ClassCommentServiceInter classCommentServiceImpl;
    @Resource
    private ClasstServiceInter classTServiceImpl;

    public ClassCommentServiceInter getClassCommentServiceImpl() {
        return classCommentServiceImpl;
    }

    public void setClassCommentServiceImpl(ClassCommentServiceInter classCommentServiceImpl) {
        this.classCommentServiceImpl = classCommentServiceImpl;
    }

    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> queryall(DataQuery query,
                                        HttpServletRequest request) {

        try {
            query = EasyUiUtils.formatGridQuery(request, query);
            DataResult result = classCommentServiceImpl
                .createClassCommentSimpleQuery(query).search();
            List<Map<String, Object>> list = result.getResultList();
            result.runformatTime("CTIME", "yyyy-MM-dd HH:mm:ss");
            for (Map<String, Object> map : list) {
                String classid = map.get("CLASSID").toString();
                Classt classtEntity = classTServiceImpl.getClasstEntity(classid);
                map.put("CLASSNAME", classtEntity.getName());
            }
            return ViewMode.getInstance()
                .putAttrs(EasyUiUtils.formatGridData(result))
                .returnObjMode();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setError(e.getMessage(), e)
                .returnObjMode();
        }
    }


    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> editSubmit(ClassComment entity, HttpSession session) {

        try {
            entity = classCommentServiceImpl.editClassCommentEntity(entity,
                getCurrentUser(session));
            return ViewMode.getInstance().setOperate(OperateType.UPDATE)
                .putAttr("entity", entity).returnObjMode();

        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setOperate(OperateType.UPDATE)
                .setError(e.getMessage(), e).returnObjMode();
        }
    }


    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> addSubmit(ClassComment entity, HttpSession session) {

        try {
            entity = classCommentServiceImpl.insertClassCommentEntity(entity,
                getCurrentUser(session));
            return ViewMode.getInstance().setOperate(OperateType.ADD)
                .putAttr("entity", entity).returnObjMode();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setOperate(OperateType.ADD)
                .setError(e.getMessage(), e).returnObjMode();
        }
    }


    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> delSubmit(String ids, HttpSession session) {
        try {
            for (String id : parseIds(ids)) {
                classCommentServiceImpl.deleteClassCommentEntity(id,
                    getCurrentUser(session));
            }
            return ViewMode.getInstance().returnObjMode();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ViewMode.getInstance().setError(e.getMessage(), e)
                .returnObjMode();
        }
    }

    @RequestMapping("/list")
    public ModelAndView index(HttpSession session) {
        return ViewMode.getInstance()
            .returnModelAndView("learn/ClassCommentResult");
    }

    @RequestMapping("/form")
    public ModelAndView view(RequestMode pageset, String ids) {
        try {
            switch (pageset.getOperateType()) {
                case (0): {//查看
                    return ViewMode.getInstance().putAttr("pageset", pageset)
                        .putAttr("entity", classCommentServiceImpl.getClassCommentEntity(ids))
                        .returnModelAndView("question/ClassCommentForm");
                }
                case (1): {// 新增
                    return ViewMode.getInstance().putAttr("pageset", pageset)
                        .returnModelAndView("question/ClassCommentForm");
                }
                case (2): {//修改
                    return ViewMode.getInstance().putAttr("pageset", pageset)
                        .putAttr("entity", classCommentServiceImpl.getClassCommentEntity(ids))
                        .returnModelAndView("question/ClassCommentForm");
                }
                default:
                    break;
            }
            return ViewMode.getInstance().returnModelAndView("question/ClassCommentForm");
        } catch (Exception e) {
            return ViewMode.getInstance().setError(e.getMessage(), e)
                .returnModelAndView("question/ClassCommentForm");
        }
    }
}
